﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Net;
using System.Collections;
using System.Xml;
using System.Linq;
//using System.Xml.Linq;

using CommonUtils;
using TDAL = EzDAL.Auto2;
using TDALUtils = EzDAL.Utils;
using TConst = TERP.Constants;
using TUtils = TERP.Utils;
using TError = TERP.Errors;


namespace TERP.BizITS
{
    public partial class BizWater
	{
        #region // Private Utils:
        #endregion

		#region // Public Ser_ROReport:
		public DataSet Ser_RO_Sumary(

		string strGWUserCode

		, string strGWPassword

		, string strTid

		, string strPartnerCode

		, string strPartnerUserCode

		, string strLanguageCode

		, string strDealerCodeList

		, string strFromDate

		, string strToDate

		)
		{

			#region // Temp:

			DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);

			int nTidSeq = 0;

			bool bNeedTransaction = false;

			string strFunctionName = "Ser_RO_Sumary";

            string strErrorCodeDefault = "";// TError.ErrITS.Ser_RO_Sumary;

			ArrayList alParamsCoupleError = new ArrayList(new object[]{

				"strFunctionName", strFunctionName

				, "strDealerCodeList", strDealerCodeList

				, "strFromDate", strFromDate

				, "strToDate", strToDate

				});

			#endregion

			try
			{

				#region // Init:

				_dbCarSv.AppUserID = strPartnerUserCode;

				if (bNeedTransaction) _dbCarSv.BeginTransaction();

				// Write request log:

				_log.WriteLogAsync(

				_strConfig_GwUserCode // strGwUserCodeForLog

				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog

				, strTid // strAppTid

				, strPartnerCode // strAppServiceCode

				, strPartnerUserCode // strAppUPartCode

				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

				, TError.Error.NoError // strAppErrorCode

				, strFunctionName // strAppLogType1

				, "REQUEST" // strAppLogType2

				, "" // strAppLogType3

				, strLanguageCode // strAppLanguageCode

				, "" // strAppRemark

				, TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple

				);

				// Check Valid Tid:

				myUtils_ValidateId(

				ref alParamsCoupleError // alParamsCoupleError

				, strTid // strTid

				, TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid

				);

				#endregion

				#region // Build Sql:

				ArrayList alParamsCoupleSql = new ArrayList();

				string strSqlGetData = @"
						select 
							'LS-'+ ro.RONo RONo
							, cus.PlateNo
							, ro.CusRequest
							, ro.CheckInDate
							, ro.Assistant
							, ro.Km
							, (select isnull(sum(Price*(1+VAT/100)), 0) 
								from Ser_ROPartItems
								where Ser_ROPartItems.ROID = ro.ROID) 
								+
								(select isnull(sum(Price*(1+VAT/100)), 0) 
								from Ser_ROServiceItems
								where Ser_ROServiceItems.ROID = ro.ROID)
							 Revenue
							from Ser_RO ro
								join Ser_Customer cus on cus.CusID = ro.CusID
							where
								(1 = 1)
								zzzzClauseWhereDealerCodeList
								and datediff(day, convert(datetime, '@FromDate', 20), ro.CheckInDate) >=0
								and datediff(day, ro.CheckInDate, convert(datetime, '@ToDate', 20)) >=0				
						";

				string zzzzClauseWhereDealerCodeList = SqlUtils.BuildClauseConditionList("and", "ro.DealerCode", strDealerCodeList, "|");

				strSqlGetData = StringUtils.Replace(

				strSqlGetData
				, "zzzzClauseWhereDealerCodeList", zzzzClauseWhereDealerCodeList
				, "@FromDate", strFromDate
                , "@ToDate", strToDate
				
				);

				#endregion

				#region // Get Data:

				DataSet dsGetData = _dbCarSv.ExecQuery(

				strSqlGetData

				, alParamsCoupleSql.ToArray()

				);

				dsGetData.Tables[0].TableName = "Ser_RO_Sumary";

				DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

				#endregion

				// Return Good:

				TDALUtils.DBUtils.CommitSafety(_dbCarSv);

				mdsFinal.AcceptChanges();

				return mdsFinal;

			}

			catch (Exception exc)
			{

				#region // Catch of try:

				// Rollback:

				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

				// Return Bad:

				return TUtils.CProcessException.Process(

				ref mdsFinal

				, exc

				, strErrorCodeDefault

				, alParamsCoupleError.ToArray()

				);

				#endregion

			}

			finally
			{

				#region // Finally of try:

				// Rollback and Release resources:

				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

				TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);

				TUtils.CProcessException.Process(

				ref mdsFinal // mdsFinal

				, _log // log

				, _strConfig_GwUserCode // strGwUserCodeForLog

				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog

				, strFunctionName // strFunctionName

				, TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid

				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

				, strPartnerCode // strAppPartCode

				, strPartnerUserCode // strAppUserCode

				, strLanguageCode // strLanguageCode

				, "" // strRemark"

				);

				#endregion

			}

		}

		#endregion

     }
}
